package com.gwm.starter.oss.config;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.gwm.starter.oss.props.OssProperties;
import com.gwm.starter.oss.service.GwmOssService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Configuration
@ConditionalOnClass({GwmOssService.class})
@EnableConfigurationProperties(OssProperties.class)
public class OssAutoConfigure {

    @Resource
    private OssProperties ossProperties;

    @Bean
    public OSS ossClient() {
        return new OSSClientBuilder().build(
                ossProperties.getEndpoint(),
                ossProperties.getAccessKeyId(),
                ossProperties.getAccessKeySecret()
        );
    }

    @Bean
    @ConditionalOnMissingBean
    GwmOssService gwmOssService (OSS ossClient){
        return new GwmOssService(ossClient);
    }
}
